package com.projectzqjz.parttimework.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.base.BaseFragment;
import com.projectzqjz.parttimework.entity.PartBaseEntity;
import com.projectzqjz.parttimework.net.MyCallback;
import com.projectzqjz.parttimework.net.NoValidationTask;
import com.projectzqjz.parttimework.ui.activity.PartApplyActivity;
import com.projectzqjz.parttimework.ui.activity.PartFeedBackActivity;
import com.projectzqjz.parttimework.ui.activity.PartLoginActivity;
import com.projectzqjz.parttimework.ui.activity.PartMineInfoActivity;
import com.projectzqjz.parttimework.ui.activity.PartMyCollectionActivity;
import com.projectzqjz.parttimework.ui.activity.PartMyDiaryActivity;
import com.projectzqjz.parttimework.ui.activity.PartSettingActivity;
import com.projectzqjz.parttimework.ui.activity.PartWebCommonActivity;
import com.projectzqjz.parttimework.utils.AppConfig;
import com.projectzqjz.parttimework.utils.NewsToastUtils;
import com.projectzqjz.parttimework.utils.SPUtils;
import com.projectzqjz.parttimework.utils.ScreenUtil;
import com.projectzqjz.parttimework.utils.UserUtils;
import com.projectzqjz.parttimework.widget.CenterDialog;
import com.squareup.picasso.Picasso;
import com.tencent.connect.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Response;

import static com.projectzqjz.parttimework.base.Constant.CHANGEINFO;

public class PartMineFragment extends BaseFragment {
    @BindView(R.id.view_status_bar_place)
    View statusBarPlace;
    @BindView(R.id.ll_no_login)
    LinearLayout relNoLogin;//表示没有登录
    @BindView(R.id.tv_nickName)
    TextView tvNickName;//昵称  表示已经登录

    @BindView(R.id.iv_head)
    CircleImageView iv_head;

    private boolean isFirst = true;

    private String avatar = "";
    private String nickName = "";

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        //获取出价记录  然后进行设置
        try {
            //重新刷数据
            getAccount();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_part3_mine, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (isFirst) {
                isFirst = false;
            }
            getAccount();
        }
    }

    @OnClick({R.id.rel_collect, R.id.rel_cooperation, R.id.rel_feedBack, R.id.rel_setting
            , R.id.ll_apply, R.id.ll_all, R.id.ll_done, R.id.ll_continue, R.id.tv_3, R.id.rel_account
    })
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_apply://已报名
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", -1));
                break;
            case R.id.ll_all://全部
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", -2));
                break;
            case R.id.ll_done://已完成
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", 1));
                break;
            case R.id.ll_continue://进行中
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", 0));
                break;
            case R.id.rel_collect://我的收藏
                startActivity(new Intent(getActivity(), PartMyCollectionActivity.class));
                break;
            case R.id.rel_cooperation://商务合作
                startActivity(new Intent(getActivity(), PartWebCommonActivity.class)
                        .putExtra("url", "http://jzs.uprftk.cn:8080/xrk-job/index.html#/businessCooperation ")
                        .putExtra("title", "商务合作"));
                break;
            case R.id.rel_feedBack://我的反馈
                startActivity(new Intent(getActivity(), PartFeedBackActivity.class));
                break;
            case R.id.rel_setting://设置
                startActivity(new Intent(getActivity(), PartSettingActivity.class));
                break;
            case R.id.tv_3://完善简历
                startActivity(new Intent(getActivity(), PartMyDiaryActivity.class));
                break;
            case R.id.rel_account://点击进行登录
                if (UserUtils.checkLogin()) {
                    Objects.requireNonNull(getActivity()).startActivity(new Intent(getActivity(), PartMineInfoActivity.class)
                            .putExtra("head", avatar).putExtra("name", nickName));
                } else {
                    UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
                    Objects.requireNonNull(getActivity()).startActivity(new Intent(getActivity(), PartLoginActivity.class));
                }
                break;
        }

    }

    //获取个人中心资料信息
    private void getAccount() {
        String userId = SPUtils.get(App.getContext(), "userId", "") + "";
        Dialog dialog = new CenterDialog(R.layout.loading_layout, getActivity()).showLoad("正在获取用户信息...");
        NoValidationTask.getApiService().getPart3Account(userId).enqueue(new MyCallback<PartBaseEntity>(getActivity()) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                dialog.dismiss();
                assert response.body() != null;
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                JSONObject jsonObject = JSON.parseObject(jsonStr);
                checkLogin(jsonObject);
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
            }
        });
    }

    //判断是否登录
    private void checkLogin(JSONObject jsonObject) {
        avatar = jsonObject.getString("avatar");
        nickName = jsonObject.getString("realName");
        if (UserUtils.checkLogin()) {//表示已经登录
            relNoLogin.setVisibility(View.GONE);
            tvNickName.setVisibility(View.VISIBLE);
            tvNickName.setText(nickName);
            Picasso.with(App.getContext()).load(avatar).into(iv_head);
        } else {//表示还没有登录
            relNoLogin.setVisibility(View.VISIBLE);
            tvNickName.setVisibility(View.GONE);
            Picasso.with(App.getContext()).load(avatar).into(iv_head);
        }
    }
}
